package com.mall.task.psvideo;

import java.io.File;
import java.sql.DriverManager;
import java.sql.ResultSet;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class PsInsertItemTask {
	
	public static void main(String[] args) throws Exception  {
		String path = "F:/333/";
		 File file = new File(path);   
		 File[] array = file.listFiles();   
		 Connection conn = null;
			String dbUrl = "jdbc:mysql://192.168.31.114:3306/mall_b2b?"
					+ "user=root&password=123456&useUnicode=true&characterEncoding=UTF8";
			Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动

			conn = (Connection) DriverManager.getConnection(dbUrl);
			Statement stmt = (Statement) conn.createStatement();
	        for(int i=0;i<array.length;i++){   
	            if(array[i].isFile()){   
	                String fileName =  array[i].getName();
	                System.out.println(fileName);   
	                String sqlFileName = fileName.substring(0, fileName.length()-4);//视频展示给用户的标题
	                System.out.println(sqlFileName); 
	                Integer itemId=104;//商品id
	                String code = "xj56a1";//视频提取码
	                Integer category = 36;
	                Integer zhangjie=i+1;//视频章节
	                String newName = null;//视频重命名
	                String url = "";//视频播放的url
	                
//	                String aa[] = sqlFileName.split("-");
//	                Integer headNum = Integer.parseInt(aa[0]);
	                if(i<9){
	                	newName = "0"+zhangjie+"xjai";
	                	url= "xjaisp/"+newName+".mp4";
	                }else{
	                	newName = zhangjie+"xjai";
	                	url= "xjaisp/"+newName+".mp4";
	                }
	               System.out.println(url);
	                array[i].renameTo(new File("F:\\222\\" + newName+".mp4"));
	                String sql = "insert into ps_video (zhangjie,title,url,category,code,ps_item_id,gmt_create,gmt_modify,status,type)"
	                + "	 values ('" + zhangjie + "','" + sqlFileName + "','" + url + "','" + category + "','" + code + "','" + itemId + "',now(),now(),0,0)";
	                


	    			stmt.executeUpdate(sql);
	    			ResultSet rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
	    			int id = 0;
	    			if (rs.next()) {
	    				id = rs.getInt(1);
	    			}
	    			Thread.sleep(1000);
	            } 
	        } 
	        conn.close();
	}

}
